<?php

class ReleaseList {

    protected $dbo = NULL;

    public function __construct() {

        $this->dbo  = &DBO::getInstance();
    }


    /* RECENTLY ADDED */

    public function getRecentAddedSchedules($count = 0, $user_id = null) {

        if($count <= 0) {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                ORDER BY id DESC",
                array(
                    DBInteger::create($user_id)
                ),
                DB_FETCHMODE_ASSOC);

        } else {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                ORDER BY id DESC LIMIT ?",
                array(
                    DBInteger::create($user_id),
                    DBInteger::create($count)
                ),
                DB_FETCHMODE_ASSOC);
        }

        throwOnError($res);

        return $res;
    }

    public function getRecentAddedSchedulesByTag($tag, $count = 0, $user_id = null) {

        if($count <= 0) {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id
                        AND user_id = ?)
                WHERE genre ILIKE ?
                ORDER BY id DESC",
                array(
                    DBInteger::create(isset($_SESSION['user_id']) ? $_SESSION['user_id'] : NULL),
                    DBString::create('%'.$tag.'%')
                ),
                DB_FETCHMODE_ASSOC);

        } else {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id
                        AND user_id = ?)
                WHERE genre ILIKE ?
                ORDER BY id DESC LIMIT ?",
                array(
                    DBInteger::create($user_id),
                    DBString::create('%'.$tag.'%'),
                    DBInteger::create($count)
                ),
                DB_FETCHMODE_ASSOC);
        }

        throwOnError($res);

        return $res;
    }
    
    
    
    /* RECENTLY RELEASED */

    public function getRecentReleases($count = 0) {

        if($count <= 0) {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.releases_view
                WHERE release_date IS NOT NULL
                    AND extract(year from release_date) >= 2009
                    ORDER BY release_date DESC",
                NULL,
                DB_FETCHMODE_ASSOC);

        } else {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.releases_view
                WHERE release_date IS NOT NULL
                    AND extract(year from release_date) >= 2009
                    ORDER BY release_date DESC
                    LIMIT ?",
                array(
                    DBInteger::create($count)
                ),
                DB_FETCHMODE_ASSOC);
        }

        throwOnError($res);

        return $res;
    }

    public function getRecentReleasesByTag($tag, $count = 0) {

        if($count <= 0) {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.releases_view
                WHERE release_date IS NOT NULL
                    AND extract(year from release_date) >= 2009
                    AND genre ILIKE ?
                    ORDER BY release_date DESC",
                array(
                    DBString::create('%'.$tag.'%')
                ),
                DB_FETCHMODE_ASSOC);

        } else {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.releases_view
                WHERE release_date IS NOT NULL
                    AND extract(year from release_date) >= 2009
                    AND genre ILIKE ?
                    ORDER BY release_date DESC
                    LIMIT ?",
                array(
                    DBString::create('%'.$tag.'%'),
                    DBInteger::create($count)
                ),
                DB_FETCHMODE_ASSOC);
        }

        throwOnError($res);

        return $res;
    }



    /* UPCOMING SCHEDULES */

    public function getUpcomingSchedules($count = 0, $user_id = null) {

        if($count <= 0) {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                WHERE mtl.is_schedule_date_ym(schedule_date)
                ORDER BY schedule_date ASC",
                array(
                    DBInteger::create($user_id)
                ),
                DB_FETCHMODE_ASSOC);

        } else {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                WHERE mtl.is_schedule_date_ym(schedule_date)
                ORDER BY schedule_date ASC
                LIMIT ?",
                array(
                    DBInteger::create($user_id),
                    DBInteger::create($count)
                ),
                DB_FETCHMODE_ASSOC);
        }

        throwOnError($res);

        return $res;
    }

    public function getUpcomingSchedulesByTag($tag, $count = 0, $user_id = null) {

        if($count <= 0) {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                WHERE mtl.is_schedule_date_ym(schedule_date)
                    AND genre ILIKE ?
                ORDER BY schedule_date ASC",
                array(
                    DBInteger::create($user_id),
                    DBString::create('%'.$tag.'%')
                ),
                DB_FETCHMODE_ASSOC);

        } else {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                WHERE mtl.is_schedule_date_ym(schedule_date)
                    AND genre ILIKE ?
                ORDER BY schedule_date ASC
                LIMIT ?",
                array(
                    DBInteger::create($user_id),
                    DBString::create('%'.$tag.'%'),
                    DBInteger::create($count)
                ),
                DB_FETCHMODE_ASSOC);
        }

        throwOnError($res);

        return $res;
    }







    /* RECENTLY ANNOUNCED */

    public function getAnnouncedSchedules($count = 0, $user_id = null) {

        if($count <= 0) {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                WHERE NOT mtl.is_schedule_date_ym(schedule_date)
                ORDER BY schedule_date ASC",
                array(
                    DBInteger::create($user_id)
                ),
                DB_FETCHMODE_ASSOC);

        } else {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                WHERE NOT mtl.is_schedule_date_ym(schedule_date)
                ORDER BY schedule_date ASC
                LIMIT ?",
                array(
                    DBInteger::create($user_id),
                    DBInteger::create($count)
                ),
                DB_FETCHMODE_ASSOC);
        }

        throwOnError($res);

        return $res;
    }

    public function getAnnouncedSchedulesByTag($tag, $count = 0, $user_id = null) {

        if($count <= 0) {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                WHERE NOT mtl.is_schedule_date_ym(schedule_date)
                    AND genre ILIKE ?
                ORDER BY schedule_date ASC",
                array(
                    DBInteger::create($user_id),
                    DBString::create('%'.$tag.'%')
                ),
                DB_FETCHMODE_ASSOC);

        } else {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                WHERE NOT mtl.is_schedule_date_ym(schedule_date)
                    AND genre ILIKE ?
                ORDER BY schedule_date ASC
                LIMIT ?",
                array(
                    DBInteger::create($user_id),
                    DBString::create('%'.$tag.'%'),
                    DBInteger::create($count)
                ),
                DB_FETCHMODE_ASSOC);
        }

        throwOnError($res);

        return $res;
    }

    public function getReleasesByIds($ids = array(), $count = 0, $user_id = null) {

        $id_str = implode(',', $ids);

        if(empty($id_str))
            return array();

        if($count <= 0) {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                WHERE schedules_view.id IN (!)
                ORDER BY schedules_view.schedule_date ASC",
                array(
                    DBInteger::create($user_id),
                    DBString::create($id_str),
                ),
                DB_FETCHMODE_ASSOC);

        } else {

            $res = $this->dbo->getAll("SELECT *
                FROM mtl.schedules_view
                LEFT OUTER JOIN mtl.watch_releases
                    ON (watch_releases.release_id = schedules_view.id AND user_id = ?)
                WHERE schedules_view.id IN (!)
                ORDER BY schedules_view.schedule_date ASC
                LIMIT ?",
                array(
                    DBInteger::create($user_id),
                    DBString::create($id_str),
                    DBInteger::create($count)
                ),
                DB_FETCHMODE_ASSOC);
        }

        throwOnError($res);

        return $res;
    }



}